// 空图片默认指向官方网站，但官网已不存在该图片。
Ext.BLANK_IMAGE_URL = 'resources/images/default/s.gif';

// IE8下DateField目录显示不全
Ext.isIE8 = Ext.isIE && navigator.userAgent.indexOf('MSIE 8') != -1;
Ext.override(Ext.menu.Menu, {
	autoWidth : function() {
		var el = this.el, ul = this.ul;
		if (!el) {
			return;
		}
		var w = this.width;
		if (w) {
			el.setWidth(w);
		} else if (Ext.isIE && !Ext.isIE8) {
			el.setWidth(this.minWidth);
			var t = el.dom.offsetWidth; // force recalc
			el.setWidth(ul.getWidth() + el.getFrameWidth("lr"));
		}
	}
});

// GridPanel功能新增和问题修复
if (!Ext.grid.GridView.prototype.templates) {
	Ext.grid.GridView.prototype.templates = {};
}
// 使得GridPanel单元格中的文字鼠标可选
Ext.grid.GridView.prototype.templates.cell = new Ext.Template(
		"<td class=\"x-grid3-col x-grid3-cell x-grid3-td-{id} {css}\" style=\"{style}\" tabIndex=\"0\" {cellAttr}>",
		"<div class=\"x-grid3-cell-inner x-grid3-col-{id}\" style=\"-moz-user-select:text;-khtml-user-select:text;\" {attr}>{value}</div>",
		"</td>")
// FireFox下单元格中长文本无法正确计算宽度和添加省略号
Ext.grid.GridView.prototype.templates.header = new Ext.Template(
		"<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"table-layout:fixed;{tstyle}\">",
		"<thead><tr class=\"x-grid3-hd-row\">{cells}</tr></thead>", "</table>");
Ext.grid.GridView.prototype.templates.row = new Ext.Template(
		"<div class=\"x-grid3-row {alt}\" style=\"{tstyle}\"><table class=\"x-grid3-row-table\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" style=\"table-layout:fixed;{tstyle}\">",
		"<tbody><tr>{cells}</tr>",
		(this.enableRowBody ? "<tr class=\"x-grid3-row-body-tr\" style=\"{bodyStyle}\"><td colspan=\"{cols}\" class=\"x-grid3-body-cell\" tabIndex=\"0\" hidefocus=\"on\"><div class=\"x-grid3-row-body\">{body}</div></td></tr>"
				: ""), "</tbody></table></div>");
// PageToolbar可设置每页显示多少条记录
Ext.apply(Ext.PagingToolbar.prototype, {
	// 新添加代码开始
	beforePageSizeText : "Displaying",
	afterPageSizeText : "record(s) per page",
	// 新添加代码结束
	onRender : function(B, A) {
		Ext.PagingToolbar.superclass.onRender.call(this, B, A);
		this.first = this.addButton({
			tooltip : this.firstText,
			iconCls : "x-tbar-page-first",
			disabled : true,
			handler : this.onClick.createDelegate(this, [ "first" ])
		});
		this.prev = this.addButton({
			tooltip : this.prevText,
			iconCls : "x-tbar-page-prev",
			disabled : true,
			handler : this.onClick.createDelegate(this, [ "prev" ])
		});
		this.addSeparator();
		this.add(this.beforePageText);
		this.field = Ext.get(this.addDom({
			tag : "input",
			type : "text",
			size : "3",
			value : "1",
			cls : "x-tbar-page-number"
		}).el);
		this.field.on("keydown", this.onPagingKeydown, this);
		this.field.on("focus", function() {
			this.dom.select();
		});
		this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
		this.field.setHeight(18);
		this.addSeparator();
		this.next = this.addButton({
			tooltip : this.nextText,
			iconCls : "x-tbar-page-next",
			disabled : true,
			handler : this.onClick.createDelegate(this, [ "next" ])
		});
		this.last = this.addButton({
			tooltip : this.lastText,
			iconCls : "x-tbar-page-last",
			disabled : true,
			handler : this.onClick.createDelegate(this, [ "last" ])
		});
		this.addSeparator();
		this.loading = this.addButton({
			tooltip : this.refreshText,
			iconCls : "x-tbar-loading",
			handler : this.onClick.createDelegate(this, [ "refresh" ])
		});
		if (this.displayInfo) {
			this.displayEl = Ext.fly(this.el.dom).createChild({
				cls : "x-paging-info"
			});
		}
		if (this.dsLoaded) {
			this.onLoad.apply(this, this.dsLoaded);
		}
		// 新添加代码
		this.addSeparator();
		this.add(this.beforePageSizeText);
		this.field2 = Ext.get(this.addDom({
			tag : "input",
			type : "text",
			size : "10",
			value : this.pageSize,
			cls : "x-tbar-page-number"
		}).el);
		this.field2.on("keydown", this.onPagingKeydown2, this);
		this.field2.on("focus", function() {
			this.dom.select();
		});
		this.add(this.afterPageSizeText);
		this.field2.setHeight(18);
	},
	// 新添加代码：处理设置每页显示多少条记录的输入框
	onPagingKeydown2 : function(D) {
		var B = D.getKey(), C = this.field2.dom.value;
		if (B == D.RETURN) {
			D.stopEvent();
			if (!C || isNaN(C)) {
				this.field2.dom.value = this.pageSize;
			} else {
				this.pageSize = Number(C);
				this.doLoad(0);
			}
		}
	},
	onLoad : function(A, C, F) {
		if (!this.rendered) {
			this.dsLoaded = [ A, C, F ];
			return
		}
		this.cursor = F.params ? F.params[this.paramNames.start] : 0;
		var E = this.getPageData(), B = E.activePage, D = E.pages;
		this.afterTextEl.el.innerHTML = String.format(this.afterPageText,
				E.pages);
		this.field.dom.value = B;
		// 如果只输入值但没回车设置pagesize，则恢复正确值。
		this.field2.dom.value = this.pageSize;
		this.first.setDisabled(B == 1);
		this.prev.setDisabled(B == 1);
		this.next.setDisabled(B == D);
		this.last.setDisabled(B == D);
		this.loading.enable();
		this.updateInfo();
	}
});
// 改写并简化EXT当中examples带的消息提示
Ext.example = function() {
	var msgCt;
	function createBox(t, s) {
		return [
				'<div class="msg">',
				'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
				'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>',
				t,
				'</h3>',
				s,
				'</div></div></div>',
				'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
				'</div>' ].join('');
	}
	return {
		msg : function(title, format) {
			if (!msgCt) {
				msgCt = Ext.DomHelper.insertFirst(document.body, {
					id : 'msg-div'
				}, true);
			}
			msgCt.alignTo(document, 't-t');
			var s = String.format.apply(String, Array.prototype.slice.call(
					arguments, 1));
			var m = Ext.DomHelper.append(msgCt, {
				html : createBox(title, s)
			}, true);
			m.slideIn('t').pause(1).ghost("t", {
				remove : true
			});
		}
	};
}();